package cn.edu.tjufe.llp.tin.controller.part02;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Controller
@RequestMapping("/part02")
@Tag(name = "带有参数的接口演示")
public class ParamController {
	
	@Operation(summary = "带有普通参数的接口", description = "演示接口参数以及ModelMap")
	@GetMapping("param01")
	public String printParam01(@RequestParam("name") String name, ModelMap model) {
		//1.参数与变量名称相同时，不需要设置@RequestParam的value
		//2.GET方法传来的参数，可以是中文，不会乱码
		
		model.addAttribute("message", "Hello " + name);
		return "/html/part02/param";
	}
	
	@Operation(summary = "带有@PathVariable参数的接口", description = "演示@PathVariable参数以及ModelMap")
	@GetMapping("param02/{name}")
	public String printParam02(@PathVariable("name") String name, ModelMap model) {
		model.addAttribute("message", "Hello " + name);
		return "/html/part02/param";
	}
	
	@Operation(summary = "非必须参数，默认值以及格式化", description = "演示非必须参数，默认值以及@NumberFormat")
	// @RequestMapping("param03")     // 同时支持多种不同的method
	@GetMapping("param03")
	public String printParam03(@RequestParam(required=false, value="name", defaultValue="") String name, 
			@RequestParam(required=false) @NumberFormat(pattern="0.00") BigDecimal price,
			ModelMap model) {
		model.addAttribute("message", "Hello " + name);
		model.addAttribute("price", price);
		return "/html/part02/param";
	}
	
	@Operation(summary = "得到带有<form>的页面", description = "为了演示POST方法，用GET方法取得一个带有<form>的页面")
	@GetMapping("param04Pre")
	public String printParam04Pre() {
		return "/html/part02/paramPost";
	}	

	@Operation(summary = "演示POST方法", description = "POST方法提交数据")
	@PostMapping("param04") 
	public String printParam04(@RequestParam(required=false, value="name", defaultValue="") String name, 
			@RequestParam(required=false) @NumberFormat(pattern="0.00") BigDecimal price,
			ModelMap model) {
		
		model.addAttribute("name", name);
		model.addAttribute("price", price);
		return "/html/part02/paramPost";
	}	

	@Operation(summary = "@RequestMapping支持多种方法", description = "一般用在Controller前面，很少用在方法前面")
	@RequestMapping("param05")     // 同时支持多种不同的method
	public String printParam05(@RequestParam(required=false, value="name", defaultValue="") String name, 
			@RequestParam(required=false) @NumberFormat(pattern="0.00") BigDecimal price,
			ModelMap model) {
		
		model.addAttribute("message", "Hello " + name);
		model.addAttribute("price", price);
		return "/html/part02/param";
	}


	@Operation(summary = "@DateTimeFormat和LocalDateTime的使用", description = "日期时间格式比较灵活，在很多地方需要指定格式。")
	@RequestMapping("param06")     // 同时支持多种不同的method
	public String printParam06(@RequestParam(required=false, value="name", defaultValue="") String name, 
			@RequestParam(required=false) @NumberFormat(pattern="0.00") BigDecimal price,
			@RequestParam(required=false) @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime dob1,
			@RequestParam(required=false) @DateTimeFormat(iso = ISO.DATE_TIME) LocalDateTime dob2,
			ModelMap model) {
		
		model.addAttribute("message", "Hello " + name + "dob1=" + dob1 + "dob2=" + dob2);
		model.addAttribute("price", price);
		return "/html/part02/param";
	}
}
